﻿@{
    Layout = "~/Views/Shared/_Index.cshtml";
 }
<div class="container-div">
    <div class="row">
        <div id="searchDiv" class="col-sm-12 search-collapse">
            <div class="select-list">
                <ul>
                    <li>
                        ProductId：<input id="productId" col="ProductId" type="text" />
                    </li>
                    <li>
                        <a id="btnSearch" class="btn btn-primary btn-sm" onclick="searchGrid()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="btn-group-sm hidden-xs" id="toolbar">
            <a id="btnAdd" class="btn btn-success" onclick="showSaveForm(true)"><i class="fa fa-plus"></i> 新增</a>
            <a id="btnEdit" class="btn btn-primary disabled" onclick="showSaveForm(false)"><i class="fa fa-edit"></i> 修改</a>
            <a id="btnEdit" name="btnEdit" class="btn btn-primary disabled" onclick="changeStatus(1)"> 提交</a>
            <a id="btnReview" name="btnReview" class="btn btn-success disabled" onclick="changeStatus(3)"> 审核通过</a>
            <a id="btnReview" name="btnReview" class="btn btn-danger disabled" onclick="changeStatus(2)"> 审核拒绝</a>
            <a id="btnConfirm" name="btnConfirm" class="btn btn-success disabled" onclick="changeStatus(4)"> 批准</a>
            <a id="btnConfirm" name="btnConfirm" class="btn btn-success disabled" onclick="changeStatus(5)"> 批准拒绝</a>
            <a id="btnDelete" class="btn btn-danger disabled" onclick="deleteForm()"><i class="fa fa-remove"></i> 删除</a>
            <a id="btnFlow" class="btn btn-info disabled" onclick="showFlowForm()"><i class="fa fa-bars"></i> 查看流转</a>
            <a id="btnReport" class="btn btn-info disabled" onclick="showPreviewForm(false)"><i class="fa fa-area-chart"></i> 打印报告</a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="gridTable" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>

<script type="text/javascript">
    var user = [];
    var opruser = @Html.Raw(ViewBag.User);
    var userId = opruser.UserId;
    $(function () {
        initGrid();
    });

    function initGrid() {
        ys.ajax({
            url: '@Url.Content("~/OrganizationManage/User/GetListJson")',
            async: false,
            success: function (obj) {
                if (obj.Tag == 1) {
                    user = obj.Result;
                }
                else {
                    ys.msgError(obj.Message);
                }
            }
        });
        var queryUrl = '@Url.Content("~/ExperimentManage/ExperimentData/GetPageListJson")';
        $('#gridTable').ysTable({
            url: queryUrl,
            columns: [
                { checkbox: true, visible: true },
                { field: 'Id', title: 'Id', visible: false},
                { field: 'ExperimentNumber', title: '编号'  },
                { field: 'SumbitDatetime', title: '提交日期', formatter: function (value, row, index) { return value; } },
                { field: 'AccountName', title: '客户' },
                {
                    field: 'OwnerIds', title: '试验人', formatter: function (value, row, index) {
                        var user = findUser(value);
                        if (user) {
                            return findUser(value).UserName;
                        }
                        return null;
                    }
                },
                {
                    field: 'ReviewIds', title: '审核人', formatter: function (value, row, index) {
                        var user = findUser(value);
                        if (user) {
                            return findUser(value).UserName;
                        }
                        return null;
                    }
                },
                {
                    field: 'ConfirmIds', title: '批准人', formatter: function (value, row, index) {
                        var user = findUser(value);
                        if (user) {
                            return findUser(value).UserName;
                        }
                        return null;
                    }
                },
                { field: 'DeviceNo', title: '设备编号' },
                { field: 'DeviceLicNo', title: '序列号' },
                {
                    field: 'Status', title: '状态', formatter: function (value, row, index) {
                        var str = "";
                        switch (value) {
                            case 0:
                                str = "待提交";
                                break;
                            case 1:
                                str = "待审核";
                                break;
                            case 2:
                                str = "审核拒绝";
                                break;
                            case 3:
                                str = "待批准";
                                break;
                            case 4:
                                str = "已批准";
                                break;
                            case 5:
                                str = "批准拒绝";
                                break;
                        }
                        return str;
                    }
                }
            ],
            queryParams: function (params) {
                var pagination = $('#gridTable').ysTable('getPagination', params);
                var queryString = $('#searchDiv').getWebControls(pagination);
                return queryString;
            }
        });

    }

    function searchGrid() {
        $('#gridTable').ysTable('search');
        resetToolbarStatus();
    }

    function findUser(uId) {
        for (var i in user) {
            if (user[i].Id == uId) {
                return user[i];
            }
        }
    }

    function showSaveForm(bAdd) {
        var id = 0;
        var exptype = '';
        if (!bAdd) {
            var selectedRow = $('#gridTable').bootstrapTable('getSelections');
            if (!ys.checkRowEdit(selectedRow)) {
                return;
            }
            else {
                id = selectedRow[0].Id;
                exptype = selectedRow[0].ExperimentType;
                switch (exptype) {
                    case "生物安全柜":
                        window.open('@Url.Content("~/ExperimentManage/ExperimentData/ExperimentDataForm")' + '?id=' + id, 'newwindow');
                        break;
                    case "洁净工作台":
                        window.open('@Url.Content("~/ExperimentManage/ExperimentData/ExperimentCleanForm")' + '?id=' + id, 'newwindow');
                        break;
                }
            }
        }
        else {
            ys.openDialog({
                title: '选择模板类型',
                content: '@Url.Content("~/ExperimentManage/ExperimentData/SelectExpTypeForm")',
                width: '450px',
                height: '300px',
                callback: function (index, layero) {
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    iframeWin.saveForm(index);
                }
            });
        }
    }

    function changeStatus(status) {
        var selectedRow = $('#gridTable').bootstrapTable('getSelections');
        if (!ys.checkRowEdit(selectedRow)) {
            return;
        }
        var str;
        switch (status) {
            case 1: str = '提交'; break;
            case 2: str = '审核拒绝'; break;
            case 3: str = '审核通过'; break;
            case 4: str = '批准'; break;
            case 5: str = '批准拒绝'; break;
        }
        ys.confirm('确认要' + str + '这条数据吗？', function () {
            var id = selectedRow[0].Id;
            var postData = { id: id, status: status };
            var flow = {};
            flow.ExperimentDataId = id;
            flow.Remark = "原始数据" + str + "了";
            ys.ajax({
                url: '@Url.Content("~/ExperimentManage/ExperimentData/ChangeStatus")',
                type: 'post',
                data: postData,
                success: function (obj) {
                    if (obj.Tag == 1) {
                        ys.ajax({
                            url: '@Url.Content("~/ExperimentManage/ExperimentFlow/SaveFormJson")',
                            type: 'post',
                            data: flow,
                            success: function (obj) {
                                if (obj.Tag == 1) {
                                    ys.msgSuccess("操作成功");
                                    searchGrid();
                                }
                                else {
                                    ys.msgError(obj.Message);
                                }
                            }
                        });
                    }
                    else {
                        ys.msgError(obj.Message);
                    }
                }
            });
        });
    }

    function deleteForm() {
        var selectedRow = $('#gridTable').bootstrapTable('getSelections');
        if (ys.checkRowDelete(selectedRow)) {
            ys.confirm('确认要删除选中的' + selectedRow.length + '条数据吗？', function () {
                var password = prompt("请输入登录密码", "");
                var postData = {};
                postData.UserName = "guest";
                postData.Password = password;
                ys.ajax({
                    url: '@Url.Content("~/Home/AuthPsdJson")',
                    type: "Post",
                    data: postData,
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            var ids = ys.getIds(selectedRow);
                            ys.ajax({
                                url: '@Url.Content("~/ExperimentManage/ExperimentData/DeleteFormJson")' + '?ids=' + ids,
                                type: 'post',
                                success: function (obj) {
                                    if (obj.Tag == 1) {
                                        ys.msgSuccess(obj.Message);
                                        searchGrid();
                                    }
                                    else {
                                        ys.msgError(obj.Message);
                                    }
                                }
                            });
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
            });
        }
    }

    function showFlowForm() {
        var selectedRow = $('#gridTable').bootstrapTable('getSelections');
        if (!ys.checkRowEdit(selectedRow)) {
            return;
        }
        else {
            id = selectedRow[0].Id;
        }
        ys.openDialog({
            title: "查看流转",
            content: '@Url.Content("~/ExperimentManage/ExperimentFlow/ExperimentFlowForm")' + '?id=' + id,
            width: '1000px',
            height: '700px',
            btn: [],
            callback: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.saveForm(index);
            }
        });
    }

    function showPreviewForm() {
        var id = 0;
        var exptype = '';
        var selectedRow = $('#gridTable').bootstrapTable('getSelections');
        if (!ys.checkRowEdit(selectedRow)) {
            return;
        }
        else {
            id = selectedRow[0].Id;
            exptype = selectedRow[0].ExperimentType;
            switch (exptype) {
                case "生物安全柜":
                    window.open('@Url.Content("~/ExperimentManage/ExperimentData/ExperimentDataPreview")' + '?id=' + id, 'newwindow');
                    break;
                case "洁净工作台":
                    window.open('@Url.Content("~/ExperimentManage/ExperimentData/ExperimentCleanPreview")' + '?id=' + id, 'newwindow');
                    break;
            }
        }
    }
</script>
